package com.gopay.common.cps.service.autosettle.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.common.PageInfo;
import com.gopay.common.cps.manager.auosettle.AutoSettleDailyStatsAssembleManager;
import com.gopay.common.cps.manager.auosettle.AutoSettleDailyStatsAssembleVOManager;
import com.gopay.common.cps.manager.auosettle.AutoSettleDailyStatsManager;
import com.gopay.common.cps.service.autosettle.AutoSettleDailyStatsService;
import com.gopay.common.domain.autosettle.AsBankSettleDailyStats;
import com.gopay.common.domain.autosettle.bo.AutoSettleBankDailyStatsBO;
import com.gopay.common.domain.autosettle.vo.AutoSettleBankDailyStatsVO;
import com.gopay.common.util.CalendarUtils;


/** 
 * @fileName  AutoSettleDailyStatsServiceImpl.java
 * @author  张建龙 
 * @date 创建时间：2016年2月1日 下午2:02:46
 */
@Service("autoSettleDailyStatsService")
public class AutoSettleDailyStatsServiceImpl implements AutoSettleDailyStatsService {
	
	private final Logger logger = LoggerFactory.getLogger(AutoSettleDailyStatsServiceImpl.class);
	
	@Resource(name="autoSettleDailyStatsManager")
	private AutoSettleDailyStatsManager autoSettleDailyStatsManager;
	@Resource(name="autoSettleDailyStatsAssembleManager")
	private AutoSettleDailyStatsAssembleManager autoSettleDailyStatsAssembleManager;
	@Resource(name="autoSettleDailyStatsAssembleVOManager")
	private AutoSettleDailyStatsAssembleVOManager autoSettleDailyStatsAssembleVOManager;
	
	@Override
	public List<AutoSettleBankDailyStatsVO> query(PageInfo pageInfo) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("deprecation")
	@Override
	public List<AutoSettleBankDailyStatsVO> query(PageInfo pageInfo, String bankCode) {
		
		Date date = new Date();
    	date.setHours(0);
    	date.setMinutes(0);
    	date.setSeconds(0);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MILLISECOND, 000);
		int weekday = CalendarUtils.getWeekday(calendar);
		calendar.add(Calendar.DAY_OF_YEAR, -weekday-6);
		Date beginDate = calendar.getTime();
		calendar.add(Calendar.DAY_OF_YEAR, 13);
		calendar.set(Calendar.HOUR_OF_DAY, 24);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date endDate = calendar.getTime();
		
		logger.info("银行代码："+bankCode+"开始时间："+beginDate+"结束时间："+endDate);
		
		List<AsBankSettleDailyStats> asBankSettleDailyStatsList = autoSettleDailyStatsManager
				.getAsBankSettleDailyStats(beginDate, endDate, bankCode);
		logger.info("银行代码为{}的银行，上周和本周的出款实际记录数据为{}",bankCode,asBankSettleDailyStatsList);
		
		List<AutoSettleBankDailyStatsBO> autoSettleBankDailyStatsBOList = autoSettleDailyStatsAssembleManager
				.assembleAutoSettleBankDailyStatsBO(asBankSettleDailyStatsList, beginDate, endDate);
		logger.info("银行代码为{}的银行，上周实际、本周预计和本周实际的出款记录为{}",bankCode,autoSettleBankDailyStatsBOList);
		
		List<AutoSettleBankDailyStatsVO> autoSettleBankDailyStatsVOList = autoSettleDailyStatsAssembleVOManager
				.buildVoFromAsDailyStatsBo(autoSettleBankDailyStatsBOList);
		pageInfo.setTotalCount(autoSettleBankDailyStatsVOList.size());
		return autoSettleBankDailyStatsVOList;
	}
	
	@Override
	public BigDecimal queryPendingSettleAMT(Date date, String bankCode) {
		logger.info("银行代码："+bankCode+"查询时间："+date);
		date.setHours(0);
    	date.setMinutes(0);
    	date.setSeconds(0);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MILLISECOND, 000);
		int curWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
		int weekday = CalendarUtils.getWeekday(calendar);
		calendar.add(Calendar.DAY_OF_YEAR, -weekday-6);
		Date beginDate = calendar.getTime();
		calendar.add(Calendar.DAY_OF_YEAR, 13);
		calendar.set(Calendar.HOUR_OF_DAY, 24);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date endDate = calendar.getTime();
		
		logger.info("银行代码："+bankCode+"开始时间："+beginDate+"结束时间："+endDate);
		
		List<AsBankSettleDailyStats> asBankSettleDailyStatsList = autoSettleDailyStatsManager
				.getAsBankSettleDailyStats(beginDate, endDate, bankCode);
		logger.info("银行代码为{}的银行，上周和本周的出款实际记录数据为{}",bankCode,asBankSettleDailyStatsList);
		
		List<AutoSettleBankDailyStatsBO> autoSettleBankDailyStatsBOList = autoSettleDailyStatsAssembleManager
				.assembleAutoSettleBankDailyStatsBO(asBankSettleDailyStatsList, beginDate, endDate);
		logger.info("银行代码为{}的银行，上周实际、本周预计和本周实际的出款记录为{}",bankCode,autoSettleBankDailyStatsBOList);
		
		BigDecimal expectedAmt = new BigDecimal("0.00");
		switch (curWeekDay) {
		case Calendar.MONDAY:
			expectedAmt = autoSettleBankDailyStatsBOList.get(1).getMondayAmt();
			break;
		case Calendar.TUESDAY:
			expectedAmt = autoSettleBankDailyStatsBOList.get(1).getTuesdayAmt();
			break;
		case Calendar.WEDNESDAY:
			expectedAmt = autoSettleBankDailyStatsBOList.get(1).getWednesdayAmt();
			break;
		case Calendar.THURSDAY:
			expectedAmt = autoSettleBankDailyStatsBOList.get(1).getThursdayAmt();
			break;
		case Calendar.FRIDAY:
			expectedAmt = autoSettleBankDailyStatsBOList.get(1).getFridayAmt();
			break;
		case Calendar.SATURDAY:
			expectedAmt = autoSettleBankDailyStatsBOList.get(1).getSaturdayAmt();
			break;
		case Calendar.SUNDAY:
			expectedAmt = autoSettleBankDailyStatsBOList.get(1).getSundayAmt();
			break;
		default:
			break;
		}
		
		return expectedAmt;
	}

	@Override
	public Map<String, String> getJpgridUserData(List<AutoSettleBankDailyStatsVO> autoSettleBankDailyStatsVOList) {
		Map<String,String> map = new HashMap<String,String>();
		int data =autoSettleDailyStatsAssembleVOManager.caculateShowColumns(autoSettleBankDailyStatsVOList);
		map.put("showFlag", String.valueOf(data));
		return map;
	}

	

}
